import java.text.NumberFormat;
import java.util.Scanner;

/**
 * 【条件语句】【循环语句】
 * 【if...else】【for...】
 */

public class Calculator2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入本金:");
        double p = scanner.nextDouble();    //p为贷款本金principal

        if (p <= 0) {
            System.out.println("贷款金额必须>0");
            return;
        }
        System.out.println("请输入年利率:");
        double yr = scanner.nextDouble();   //yr为年利率

        if (yr < 1 || yr > 36) {
            System.out.println("年利率必须在1~36之间");
            return;
        }
        double mr = yr / 12.0 / 100;        //mr为月利率
        System.out.println("请输入还款月数:");
        int m = scanner.nextInt();          //m为还款月数

        if (m < 1 || m > 360) {
            System.out.println("还款月数必须在1-360之间");
            return;
        }
        double pow = Math.pow(1 + mr, m);
        double payment = p * mr * pow / (pow - 1);
        String format = NumberFormat.getCurrencyInstance().format(payment); //格式化计算结果为货币格式
        System.out.println("每月需还款:" + format);

        for (int i = 0; i < m; i++) {
            double payInterest = p * mr;                 //偿还利息
            double payPrincipal = payment - payInterest; //偿还本金
            p = p - payPrincipal;                        //剩余本金

            System.out.print("月份:" + (i + 1));
            System.out.print("\t本月还款:"+NumberFormat.getCurrencyInstance().format(payment));
            System.out.print("\t偿还本金:"+NumberFormat.getCurrencyInstance().format(payPrincipal));
            System.out.print("\t偿还利息:"+NumberFormat.getCurrencyInstance().format(payInterest));
            System.out.println("\t剩余本金:"+NumberFormat.getCurrencyInstance().format(p));
        }

        System.out.println("还款总额:"+NumberFormat.getCurrencyInstance().format(payment*m));
    }
}
